﻿using System;
using System.Windows;
using System.Windows.Forms;
using TfsComLib.ItemBrowser;
using TfsComLib.TfsBranchLib2;
using TextBox = System.Windows.Controls.TextBox;
using UserControl = System.Windows.Controls.UserControl;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Interaction logic for BranchListProviderSettings.xaml
    /// </summary>
    public partial class SourceControlBasePathSettings : UserControl
    {
        private ISourceControlBasePath sourceControlBasePath;

        public SourceControlBasePathSettings()
        {
            InitializeComponent();
        }

        public SourceControlBasePathSettings(ISourceControlBasePath sourceControlBasePath)
            : this()
        {
            Initialize(sourceControlBasePath);
        }

        public void Initialize(ISourceControlBasePath sourceControlBasePath)
        {
            this.sourceControlBasePath = sourceControlBasePath;
            basePath.DataContext = sourceControlBasePath;
            basePath.SetBinding(TextBox.TextProperty, "BasePath");
            tfsProperties.SetBinding(sourceControlBasePath.TfsConnection);
        }

        private void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var cid = new ChooseItemDialog(sourceControlBasePath.TfsConnection.Collection, basePath.Text))
                {
                    cid.ShowDialog();
                    if (cid.DialogResult == DialogResult.OK)
                    {
                        sourceControlBasePath.BasePath = cid.GetSelectedItem();
                    }
                }
            }
            catch 
            {
                // suppress error
                //sourceControlBasePath.IsErrorOrConnectionProblem = true;
                //sourceControlBasePath.ServiceStatus = ex.ToString();
            }
        }
    }
}